package com.huazai.service.impl;

import com.huazai.bean.po.User;
import com.huazai.dao.UserMapper;
import com.huazai.service.UserService;
import com.huazai.util.IdUtil;
import com.huazai.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author EZTTU
 */
@Service
@Qualifier("UserServiceImpl")
public class UserServiceImpl implements UserService {
    public static final String LOGIN_SUCCESS = "login success";
    public static final String USER_NO_EXIST = "user no exist";
    public static final String USER_NO_ACTIVE = "user no active";
    public static final String PASSWORD_ERROR = "password error";
    public static final String SIGN_SUCCESS = "sign success";
    public static final String SIGN_FAILED = "sign failed";
    public static final String USER_HAS_EXIST = "user has exist";
    private static IdUtil idUtil;

    static {
        idUtil = new IdUtil(1, 1, 1);
    }

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public String validateLogin(User user) {

        List<User> userList;
        User userTemp = new User();
        //查询用户是否存在
        userTemp.setEmail(user.getEmail());
        userList = userMapper.validateLogin(userTemp);

        if (userList.size() == 0) {
            return USER_NO_EXIST;
        }
        //查询用户是否激活
        userTemp.setState(1);
        userList = userMapper.validateLogin(userTemp);
        if (userList.size() == 0) {
            return USER_NO_ACTIVE;
        }

        //密码验证
        userTemp.setPassword(user.getPassword());
        userList = userMapper.validateLogin(userTemp);

        if (userList.size() == 0) {
            return PASSWORD_ERROR;
        } else {
            System.out.println(LOGIN_SUCCESS + ":" + userList.get(0).getAuthority());
            return LOGIN_SUCCESS + ":" + userList.get(0).getAuthority();
        }
    }

    @Override
    public String userRegistration(User user) {
        List<User> userList;
        User userTemp = new User();
        //查询用户是否存在
        userTemp.setEmail(user.getEmail());
        userList = userMapper.validateLogin(userTemp);

        if (userList.size() == 0) {
            user.setState(0);
            user.setActiveCode(String.valueOf(idUtil.nextId()));
            System.out.println(user);
            Boolean result = userMapper.userRegistration(user);
            if (result) {
                //如果注册成功的话，那么就发送激活邮件。
                new MailUtil(user.getEmail(), user.getActiveCode()).run();
                return SIGN_SUCCESS;
            } else {
                return SIGN_FAILED;
            }
        } else {
            return USER_HAS_EXIST;
        }
    }

    @Override
    public Boolean activateUser(String activeCode) {
        return userMapper.activateUser(activeCode);
    }

    @Override
    public List<User> getUserCustom(String userName, String email) {
        User user = new User();
        user.setUserName(userName);
        user.setEmail(email);
        return userMapper.validateLogin(user);
    }

    @Override
    public List<User> getUserByName(String userName) {
        userName = "%" + userName + "%";
        return userMapper.getUserByName(userName);
    }

    @Override
    public Boolean updateUserMessage(User user) {
        return userMapper.updateUserMessage(user);
    }

    @Override
    public Boolean deleteUserByEmail(String email) {
        return userMapper.deleteUserByEmail(email);
    }
}
